import Model from "./Model";
import Config from "./Config";
import LevelItem from "./LevelItem";
import WxUtil from "./WxUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LevelUI extends cc.Component {

    @property(cc.Node)
    content: cc.Node = null;

    @property(cc.Prefab)
    ItemPrefab: cc.Prefab = null;

    @property(cc.Prefab)
    TopPrefab: cc.Prefab = null;

    @property(cc.Prefab)
    LevelItemFnt: cc.Prefab = null;

    onLoad() {
        Config.init()

        console.log('LevelUI')
        var top = cc.instantiate(this.TopPrefab);
        this.node.addChild(top)
        top.getChildByName('level_txt').active = false

        this.content.removeAllChildren(true);

        WxUtil.showBanner()

        for (var i = 0; i < Config.drawLevels.length - 1; i++) {
            var item = cc.instantiate(this.ItemPrefab);
            item.x = i % 4 * 160 - 240
            item.y = -68 - Math.floor(i / 4) * 160
            this.content.addChild(item)
            item.getComponent(LevelItem).level = i + 1
            if (i + 1 <= Model.game.drawLevel) {
                item.getChildByName('star1').active = Model.game.drawLevelInfo[i] >= 1
                item.getChildByName('star2').active = Model.game.drawLevelInfo[i] >= 2
                item.getChildByName('star3').active = Model.game.drawLevelInfo[i] == 3
            } else {
                item.getChildByName('btn_lock').active = true
                item.getChildByName('btn').active = false
                item.getChildByName('star1').active = false
                item.getChildByName('star2').active = false
                item.getChildByName('star3').active = false
            }

            item.getChildByName('btn').on(cc.Node.EventType.TOUCH_END, this.itemClick, this)



        }
        for (var i = 0; i < Config.drawLevels.length - 1; i++) {
            if (i + 1 <= Model.game.drawLevel) {
                var fnt = cc.instantiate(this.LevelItemFnt);
                fnt.x = i % 4 * 160 - 240
                fnt.y = -38 - Math.floor(i / 4) * 160
                this.content.addChild(fnt)
                fnt.getComponent(cc.Label).string = (i + 1) + ''
            }


        }
    }
    itemClick(event) {
        var level = event.target.parent.getComponent(LevelItem).level;
        Model.game.selectedLevel = level;
        cc.director.loadScene('gameDraw')
    }

    onDestroy() {
        WxUtil.destroyBanner()
    }

}
